﻿package code.admin 
{
	import flash.events.*;
	import flash.display.*;
	public class GemWin extends Sprite
	{
		var gemwin_act = 0;
		var gemwin_st;
		var gemwin_elapsed;
		var gemwin_x;
		var gemwin_y;
		var gemwin_vx;
		var gemwin_vy;
		var gemwin_speed;
		var glob_vx = 0;
		var glob_vy = 0;
		var glob_speed = 0;
		var gemwin_area = this;
		public var _speedDisipation = 1/ 300;
		
		/*
		*
		*/
		public function GemWin()
		{
			addEventListener(Event.ADDED_TO_STAGE,gemwin_init);
			addEventListener(Event.REMOVED_FROM_STAGE,gemwin_destroy);
			setSensorArea(this);
		}
		public function gemwin_init(e=null)
		{
			trace(">>>> "+parent.stage)
			trace("GEMWIN_initiated");
			
			
			gemwin_st=new Date().getTime();
		}
		public function gemwin_destroy(e)
		{
			gemwin_area.removeEventListener(Event.ENTER_FRAME,gemwin_cycle);
		}
		public function setSensorArea(area)
		{
			trace("SETTING SENSOR AREA");
			resetListeners(gemwin_area);
			gemwin_area = area;

			
			gemwin_area.addEventListener(MouseEvent.MOUSE_DOWN,winactive);
			gemwin_area.addEventListener(MouseEvent.MOUSE_UP,wininactive);
			//parent.addEventListener(MouseEvent.MOUSE_UP,wininactive);
			gemwin_area.addEventListener(Event.ENTER_FRAME,gemwin_cycle);
			
		}
		public function winmove()
		{
			if (gemwin_act)
			{
				
				var dx = x - gemwin_x;
				var dy = y - gemwin_y;
				var d=Math.abs(Math.sqrt((dx*dx)+(dy*dy)));
				gemwin_vx = dx / d;
				gemwin_vy = dy / d;
				gemwin_speed = d / gemwin_elapsed;


			}
			gemwin_x = x;
			gemwin_y = y;
			
		}

		public function resetListeners(a)
		{
			trace("remove listeners form "+a);
			try
			{
				a.removeEventListener(MouseEvent.MOUSE_DOWN,winactive);
				a.removeEventListener(MouseEvent.MOUSE_UP,wininactive);
				//a.removeEventListener(MouseEvent.MOUSE_MOVE,winmove);
				a.removeEventListener(Event.ENTER_FRAME,gemwin_cycle);
			}
			catch (e)
			{
				trace("error");
			}

		}
		public function winactive(e)
		{

			glob_vx = 0;
			glob_speed = 0;


			gemwin_act = 1;
			//gemwin_st=new Date().getTime();
			gemwin_x = x;
			gemwin_y = y;
			this.startDrag(false);
		}
		public function wininactive(e)
		{
			gemwin_act = 0;

			this.stopDrag();
			glob_vx = gemwin_vx;
			glob_vy = gemwin_vy;
			glob_speed = gemwin_speed;

		}
		public function gemwin_cycle(e)
		{
			var tmp=new Date().getTime();
			gemwin_elapsed = tmp - gemwin_st;
			
			
			if(gemwin_act)
			{
				winmove();
			}
			
			
			if (glob_speed > 0)
			{
				if (x>0 && (x+width)<stage.stageWidth)
				{
					x +=  glob_vx * glob_speed*gemwin_elapsed;
				}
				if (y>0 && (y+height)<stage.stageHeight)
				{
					y +=  glob_vy * glob_speed*gemwin_elapsed;
				}
				glob_speed -=  _speedDisipation*gemwin_elapsed;
				//trace(glob_speed);
			}
			else
			{
				glob_speed = 0;
			}

			if (x < 0)
			{
				x = 0;
			}
			if ((x+width)>stage.stageWidth)
			{
				x = stage.stageWidth - width;
			}
			//
			if (y < 0)
			{
				y = 0;
			}
			if ((y+height)>stage.stageHeight)
			{
				y = stage.stageHeight - height;
			}
			
			gemwin_st=new Date().getTime();
		}
	}

}